ValueError:操作参数必须为str或unicode |
您所在的位置:网站首页 › ajax 返回error › ValueError:操作参数必须为str或unicode |
我正在尝试使用python将一些数据库字段插入SQLite DB。我不断收到以下错误: ValueError: operation parameter must be str下面是我的代码。 import sqlite3 cOnn= sqlite3.connect("pass.db") c = conn.cursor() #Create table password table_create_statement = "create table if not exists password(id integer primary key, site text, pass text);" c.execute(table_create_statement) #Promt user to input entry sites = str(raw_input("Enter name of the sites: ")) password = str(raw_input("Enter password: ")) #SQL statement to insert the new entry sql_statement = "insert into password values(?, ?, ?);",(None, sites, password) c.execute((sql_statement)) connection.commit() print("Password entered successfully")输出: Enter name of the sites: www.google.com Enter password: abcd Traceback (most recent call last): File "pw.py", line 39, in c.execute((sql_statement)) ValueError: operation parameter must be str or unicode
Martijn Piet.. 7 您需要将sql语句和参数作为单独的参数传递给execute()。您当前正在传递一个元组。 改为这样做: sql_statement = "insert into password values(?, ?, ?);" c.execute(sql_statement, (None, sites, password))现在sql_statement是单个字符串,参数作为第二个单独的参数传入。 注意,raw_input()已经返回一个str对象,无需str再次将其转换为。 1> Martijn Piet..: 您需要将sql语句和参数作为单独的参数传递给execute()。您当前正在传递一个元组。 改为这样做: sql_statement = "insert into password values(?, ?, ?);" c.execute(sql_statement, (None, sites, password))现在sql_statement是单个字符串,参数作为第二个单独的参数传入。 注意,raw_input()已经返回一个str对象,无需str再次将其转换为。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |